//
//  SCB_Radio_ViewController.m
//  Time
//
//  Created by dllo on 16/7/21.
//  Copyright © 2016年 司昌宝. All rights reserved.
//

#import "SCB_Radio_ViewController.h"
#import "ShuffView.h"
#import "Radio_Header_CollectionViewCell.h"
#import "TingModel.h"
#import "Radio_Ting_TableViewCell.h"
#import "Radio_Hot_TableViewCell.h"
#import "HotModel.h"

@interface SCB_Radio_ViewController ()
<
UITableViewDataSource,
UITableViewDelegate,
UICollectionViewDataSource,
UICollectionViewDelegate
>

/**
 *  radio 的主TableView
 */
@property (nonatomic,retain) UITableView *radioTableView;

/**
 *  头视图 按钮的名字数组
 */
@property (nonatomic,retain) NSArray *headerNameArray;
/**
 *  头视图 按钮的图片名
 */
@property (nonatomic,retain) NSArray *headerImageArray;
/**
 *  返回main菜单的按钮
 */
@property (nonatomic,retain) UIButton *mainButton;
/**
 *  主tableview头视图中的collectionView,用来放置按钮.
 */
@property (nonatomic,retain) UICollectionView *headerCollectionView;
// cell内需要使用的数组
/**
 *  tingCell的model数组;
 */
@property (nonatomic,retain) NSMutableArray *tingArray;
/**
 *  TopTing的model 数组 ,用的是tingCell
 */
@property (nonatomic,retain) NSMutableArray *topArray;
/**
 *  Hot Radio的model 数组
 */
@property (nonatomic,retain) NSMutableArray *hotArray;
@end

@implementation SCB_Radio_ViewController

-(void)dealloc{
    [_radioTableView release];
    [_headerNameArray release];
    [_headerImageArray release];
    [_mainButton release];
    [_headerCollectionView release];
    [_tingArray release];
    [_topArray release];
    [_hotArray release];
    _radioTableView.delegate = nil;
    _radioTableView.dataSource = nil;
    _headerCollectionView.delegate = nil;
    _headerCollectionView.dataSource = nil;
    [super dealloc];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.headerNameArray = @[@"爱情",@"旅行",@"故事",@"音乐",@"电影",@"读诗"];
    self.headerImageArray = @[@"爱心",@"旅行外出",@"故事",@"音乐",@"场景",@"考试"];
    self.tingArray = [NSMutableArray arrayWithCapacity:5];
    self.topArray = [NSMutableArray arrayWithCapacity:5];
    self.hotArray = [NSMutableArray arrayWithCapacity:5];
    
    self.navigationController.navigationBar.hidden = YES;
    
    [self handleDateForTing];
    [self handleDateForTop];
    [self handleDateForHot];
    [self createRadioTableView];
    [self createMainButton];
    [self.view bringSubviewToFront:self.mainButton];
}

-(void)handleDateForTing{
    NSString *TingUrlStr = @"http://api.pianke.me/version4.0/ting/listOfRecommend.php?sig=E8F452A64D8331F452F1C86766B33D38";
    NSString *body = @"pageSize=6";
    NSDictionary *header = @{@"Authorization":HEADER};
    
    [SCBNetTool POST:TingUrlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
        NSDictionary *dictionary = result;
        NSArray *array = [dictionary valueForKey:@"data"];
        for (NSDictionary *dic in array){
            TingModel *model = [[TingModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.tingArray addObject:model];
        }
//        NSLog(@"%@",self.tingArray);
        [self.radioTableView reloadData];
    } withFaolure:^(NSError *error) {
        NSLog(@"Ting数据抓取失败:%@",error);
    }];

}

-(void)handleDateForTop{
    NSString *TopUrlStr = @"http://api.pianke.me/version4.0/ting/list.php?sig=E8F452A64D8331F452F1C86766B33D38";
    NSString *body = @"pageSize=3&sort=2";
    NSDictionary *header = @{@"Authorization":HEADER};
    
    [SCBNetTool POST:TopUrlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
        NSDictionary *dictionary = result;
        NSArray *array = [dictionary valueForKey:@"data"];
        for (NSDictionary *dic in array){
            TingModel *model = [[TingModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.topArray addObject:model];
        }
        [self.radioTableView reloadData];
    } withFaolure:^(NSError *error) {
        NSLog(@"Top数据抓取失败%@",error);
    }];
}

-(void)handleDateForHot{
    NSString *HotUrlStr = @"http://api.pianke.me/version4.0/ting/listOfRadio.php?sig=E8F452A64D8331F452F1C86766B33D38";
    NSString *body = @"pageSize=4&sort=2";
    NSDictionary *header = @{@"Authorization":HEADER};
    
    [SCBNetTool POST:HotUrlStr withBody:body withBodyStyle:SCBBodyString withHeaderFile:header withResponse:SCBJSON withSuccess:^(id result) {
        NSDictionary *dictionary = result;
        NSArray *array = [dictionary valueForKey:@"data"];
        for (NSDictionary *dic in array) {
            HotModel *model = [[HotModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.hotArray addObject:model];
        }
        [self.radioTableView reloadData];
    } withFaolure:^(NSError *error) {
        NSLog(@"Hot抓取数据失败%@",error);
    }];
}

-(void)createMainButton{
    self.mainButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:self.mainButton];
    self.mainButton.backgroundColor = [UIColor blackColor];
    [self.mainButton setImage:[UIImage imageNamed:@"列表"] forState:UIControlStateNormal];
    
    
    [self.mainButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.width.mas_equalTo(30);
        make.bottom.equalTo(self.view.mas_bottom).offset(-20);
        make.height.mas_equalTo(30);
    }];
    [self.mainButton addTarget:self action:@selector(mainButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)mainButtonAction:(UIButton *)button{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

-(void)createRadioTableView{
    self.radioTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    [self.view addSubview:self.radioTableView];
    
    self.radioTableView.delegate = self;
    self.radioTableView.dataSource = self;
    
    [self setHeaderView];
    /**
     *  一堆重用池的注册
     */
    [self.radioTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"default_Cell"];
    
    [self.radioTableView registerClass:[Radio_Ting_TableViewCell class] forCellReuseIdentifier:@"Radio_Ting_TableView_Cell"];
    [self.radioTableView registerClass:[Radio_Hot_TableViewCell class] forCellReuseIdentifier:@"Radio_Hot_TableView_Cell"];
}
/**
 *  设置主tableView的tableHeaderView
 */
-(void)setHeaderView{
    
    ShuffView *shuffView = [[ShuffView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 200)];
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 270)];
    [headerView addSubview:shuffView];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SWIDTH / 6.0, 70);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.headerCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 200, SWIDTH, 70) collectionViewLayout:flowLayout];
    self.headerCollectionView.delegate = self;
    self.headerCollectionView.dataSource = self;
    [self.headerCollectionView registerClass:[Radio_Header_CollectionViewCell class] forCellWithReuseIdentifier:@"Radio_Header_CollectionView_Cell"];
    
    [headerView addSubview:self.headerCollectionView];
    self.radioTableView.tableHeaderView = headerView;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 6;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Radio_Header_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Radio_Header_CollectionView_Cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.buttonName = [self.headerNameArray objectAtIndex:indexPath.item];
    cell.imageName = [self.headerImageArray objectAtIndex:indexPath.item];
    return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    /**
     *  分区行数
     */
    return 1;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 50)];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 50)];
    [view addSubview:label];
    label.textAlignment = NSTextAlignmentNatural;
    switch (section) {
        case 0:
            label.text = @" 推荐Ting·Recommend Ting";
            return view;
            break;
        case 1:
            label.text = @" Top Ting";
            return view;
        case 2:
            label.text = @" 热门电台·Hot Radio";
            return view;
        default:
            label.text = @" 你好";
            view.backgroundColor = [UIColor greenColor];
            return view;
            break;
    }
   
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SWIDTH, 10)];
    view.backgroundColor = [UIColor whiteColor];
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:{
            Radio_Ting_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Radio_Ting_TableView_Cell" forIndexPath:indexPath];
            cell.backgroundColor = [UIColor grayColor];
            cell.tingArray = self.tingArray;
            return cell;
            }
            break;
        case 1:{
            Radio_Ting_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Radio_Ting_TableView_Cell" forIndexPath:indexPath];
            cell.tingArray = self.topArray;
            return cell;
            }
            break;
        case 2:{
            Radio_Hot_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Radio_Hot_TableView_Cell" forIndexPath:indexPath];
            //传值!!!!
            cell.hotArray = self.hotArray;
            return cell;
            }
            break;
        default:{
            UITableViewCell *cell6 = [tableView dequeueReusableCellWithIdentifier:@"default_Cell" forIndexPath:indexPath];
            cell6.backgroundColor = [UIColor colorWithRed:arc4random() % 255 / 255.0 green:arc4random() % 255 / 255.0 blue:arc4random() % 255 / 255.0 alpha:1.0];
            return cell6;
            }
            break;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            return (SWIDTH / 3 + 50) * 2;
            break;
        case 1:
            return SWIDTH / 3 + 50;
            break;
        case 2:
            return (SWIDTH - 10) + 100;
            break;
        default:
            return 70;
            break;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
